package com.fivefu.core.hillstone.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.hillstone.entity.DbHillstoneChannel;
import com.fivefu.core.hillstone.services.DbHillstoneChannelService;
import com.fivefu.core.hillstone.services.HillstoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 大华通道表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-08-24 14:48:12
 */
@Api(tags = "通道")
@RestController
@RequestMapping("/hillstoneChannel")
public class DbHillstoneChannelController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DbHillstoneChannelController.class);

    @Autowired
    private DbHillstoneChannelService channelService;

    @Autowired
    private HillstoneService hillstoneService;

    @ApiOperation(value = "获取实时监视URI",httpMethod = "POST",response = ResultInfo.class,notes = "获取实时监视URI")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelCode",value = "通道编码",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "subType",value = "码流类型(0:主码流、1:辅流1、2:辅流2。默认为0主码流)",paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "scheme",value = "协议类型(本级平台支持RTSP、FLV_HTTP、HLS、RTMP四种，下级平台只支持RTSP。默认RTSP)",paramType = "query", required = false, dataType = "String"),
    })
    @RequestMapping(path = "/getRealTimeMonitoringURI",method = RequestMethod.POST)
    public ResultInfo getRealTimeMonitoringURI(String channelCode, Integer subType, String scheme) {
        logger.info("获取实时监视URI请求参数为：" + JSON.toJSONString(request.getParameterMap()));
        try {
            if (StrUtils.isNull(channelCode)) {
                return ResultInfo.renderError("通道编码不能为空");
            }
            //调用登录
            hillstoneService.login();
            JSONObject result = channelService.getRealTimeMonitoringURI(channelCode, subType, scheme);
            //销毁会话
            hillstoneService.destoryToken();
            return ResultInfo.renderSuccess(result);
        } catch (Exception e) {
            logger.error("获取实时监视URI出现异常：" + e.getMessage(),e);
            return ResultInfo.renderError("获取实时监视URI失败");
        }
    }

    @ApiOperation(value = "获取回放URI",httpMethod = "GET",response = ResultInfo.class,notes = "获取回放URI")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelCode",value = "通道编码",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "stime",value = "回放开始时间",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "etime",value = "回放结束时间",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "location",value = "录像存储位置(cloud:平台录像，device:设备录像（不要使用）)",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "scheme",value = "协议类型，支持RTSP、RTMP、HLS三种。默认RTSP",paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "duration",value = "有效时间，单位为秒，最长不超过10分钟，默认10分钟",paramType = "query", required = false, dataType = "int")
    })
    @RequestMapping(path = "/getPlaybackURI",method = RequestMethod.GET)
    public ResultInfo getPlaybackURI(String channelCode,String stime,String etime,String location,String scheme,Integer duration) {
        try {
            logger.info("获取回放URI请求参数为：" + JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(channelCode)) {
                return ResultInfo.renderError("通道编码不能为空");
            }
            if (StrUtils.isNull(stime)) {
                return ResultInfo.renderError("回放开始时间不能为空");
            }
            if (StrUtils.isNull(etime)) {
                return ResultInfo.renderError("回放结束时间不能为空");
            }
            if (StrUtils.isNull(location)) {
                return ResultInfo.renderError("录像存储位置不能为空");
            }
            DbHillstoneChannel channel = channelService.queryChannelByCode(channelCode);
            if (null == channel) {
                return ResultInfo.renderError("监控点不存在");
            }
            //调用登录
            hillstoneService.login();
            JSONObject result = channelService.getPlaybackURI(channelCode, stime, etime,location,scheme,duration);
            //销毁会话
            hillstoneService.destoryToken();
            return ResultInfo.renderSuccess(result);
        } catch (Exception e) {
            logger.error("获取回放URI出现异常：" + e.getMessage(),e);
        }
        return ResultInfo.renderError("获取回放URI失败");
    }
}
